package com.weigee.ktv.v.adapter.rv;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.l.d.app.library.adapter.RvListAdapter;
import com.l.d.app.library.util.ToastUtil;
import com.weigee.ktv.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import skin.support.widget.SkinCompatImageView;
import skin.support.widget.SkinCompatTextView;

/**
 * Created by linde on 2017/4/29.
 * <p/>
 */
public class SupermarketChooseGoodsLeftAdapter extends RvListAdapter<String, SupermarketChooseGoodsLeftAdapter.Holder> {
    private int m_iSelectedItem;

    public SupermarketChooseGoodsLeftAdapter() {
        m_iSelectedItem = -1;
    }

    @NonNull
    @Override
    public List<String> onCreateNewEmptyList() {
        return new ArrayList<>();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final Context context = parent.getContext();
        final View itemView = LayoutInflater.from(context).inflate(R.layout.supermarket_frag_choose_goods___rv_item_left, parent, false);
        return new Holder(itemView);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        final String itemStr = getItem(position);
        if (TextUtils.isEmpty(itemStr)) {
            return;
        }
        final Context context = holder.itemView.getContext();
        final boolean isSelected = m_iSelectedItem == position;

        holder.tvTip.setText(itemStr);

        holder.tvTip.setSelected(isSelected);
        holder.ivDiverVertical.setVisibility(isSelected ? View.VISIBLE : View.GONE);

        holder.tvTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.makeTest(context, itemStr);

                m_iSelectedItem = position;
                notifyDataSetChanged();
            }
        });
    }


    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_tip)
        SkinCompatTextView tvTip;
        @BindView(R.id.iv_diver_vertical)
        SkinCompatImageView ivDiverVertical;

        private Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            ivDiverVertical.setVisibility(View.GONE);
        }
    }
}
